package demo1;

//牛客网:牛牛冲钻五

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        while(t-- > 0) {
            int n = in.nextInt();
            int k = in.nextInt();
            String str = in.next();
            int i = 0;
            int ret = 0;
            while(i<n) {
                if(str.charAt(i) == 'L') {
                    ret--;
                }else {
                    if(i>=2 && str.charAt(i) == 'W' && str.charAt(i-1) == 'W' && str.charAt(i-2) == 'W') {
                        ret += k;
                    }else {
                        ret += 1;
                    }
                }
                i++;
            }
            System.out.println(ret);
        }
    }
}